require(["config"],function(){
    require(["jquery","template","header","cookie"],function($,template){
        function Cart(){
            this.products = null;
            this.load();
            this.addListener();
        }   

        Cart.prototype = {
            constructor:Cart,
            load: function(){
                $.cookie.json = true;

                var products = this.products = $.cookie("cart") || [];

                if(products.length === 0){
                    $(".empty").show();
                    $(".cart-body-list").hide();
                    $(".select-all").hide();
                    return ;
                }

                $(".empty").hide();
                $(".cart-body-list").show();
                $(".select-all").show();

                var data = {pro:products};
                // console.log(data);
                var html = template("cart-template",data);

                $(".cart-box").html(html);
                this.calcTotal();

            },

            addListener: function(){
                $(".cart-box").on("click",".del",$.proxy(this.delHandler,this));
                $(".cart-box").on("click",".decrement,.increment",$.proxy(this.decIncHandler,this));
                $(".cart-box").on("blur",".num",$.proxy(this.inputHandler,this));
                $(".ck_all").on("click",$.proxy(this.ckAllHandler,this));
                $(".cart-box").on("click",".ck_prod",$.proxy(this.ckProdHandler,this));
                $(".del-ck").on("click",$.proxy(this.delCkHandler,this));
                $(".settlement").on("click",$.proxy(this.settlementHandler,this));
            },

            delHandler: function(event){
                var src = event.target;
                // console.log(src);
                var tr = $(src).parents(".item-list");

                var id = tr.find(".id").text();
                // console.log(id);

                this.products = this.products.filter(function(prod){
                    return !(prod.id === id)
                });

                $.cookie("cart",this.products,{expires:10,path:"/"});

                tr.remove();

                this.calcTotal();

                if(this.products.length === 0){
                    $(".empty").show();
                    $(".cart-body-list").hide();
                    $(".select-all").hide();
                }
            },

            decIncHandler: function(event){
                var src = event.target;

                var tr = $(src).parents(".item-list");

                var id = tr.find(".id").text();

                console.log(id);
                var product = this.products.filter(function(prod){
                    return prod.id === id;
                })[0];
                console.log(product);
                if($(src).is(".decrement")){
                    if(product.amount <= 1){
                        return ;
                    }
                    product.amount--;
                }else{
                    product.amount++;
                }
                $.cookie("cart",this.products,{expires:10,path:"/"});

                tr.find(".num").val(product.amount);
                tr.find(".sub").text((product.amount*product.lprice).toFixed(2));
                this.calcTotal();
            },

            inputHandler: function(event){
                console.log(1);
                var src = event.target;

                var tr = $(src).parents(".item-list");

                var id = tr.find(".id").text();

                var product = this.products.filter(prod=>prod.id === id)[0];

                var _amount = $(src).val();

                var reg = /^[1-9]\d*$/;

                if(!reg.test(_amount)){
                    $(src).val(product.amount);
                    return ;
                }

                
                product.amount = _amount;
                $.cookie("cart", this.products, {expires:10, path:"/"});
                
                tr.find(".sub").text(product.amount * product.lprice);
                this.calcTotal();

            },

            ckAllHandler: function(event){
                var status = $(event.target).prop("checked");
                // console.log(status);
                $(".ck_prod").prop("checked", status);
                this.calcTotal();
            },
            ckProdHandler: function(){
                var count = $(".ck_prod:checked").length;

                var status = count ===this.products.length;

                $(".ck_all").prop("checked", status);
                this.calcTotal();
            },
            delCkHandler: function(){
                var products = this.products;
                $(".ck_prod:checked").each(function(){
                    var id = $(this).parents(".item-list").find(".id").text();
                    products = products.filter(function(prod){
                        return !(prod.id === id)
                    });
                    // console.log(products);
                    $.cookie("cart",products,{expires:10,path:"/"});
                    $(this).parents(".item-list").remove();
                    
                })
                this.calcTotal();
                if(products.length === 0){
                    $(".empty").show();
                    $(".cart-body-list").hide();
                    $(".select-all").hide();
                    return ;
                }
            },
            calcTotal: function(){
                var sum = 0,
                num = 0;
                // console.log($(".ck_prod:checked"));
                $(".ck_prod:checked").each(function(){
                    // console.log(this);
                    sum += Number($(this).parents(".item-list").find(".sub").text());
                    num += Number($(this).parents(".item-list").find(".num").val());
                });

                $(".total").text(sum.toFixed(2));
                $(".all-num").text(num);
            },

            settlementHandler: function(){
                if($(".ck_prod:checked").length === 0){
                    alert("请选择需购买的商品！")
                }else{
                    var user = $.cookie("login-user") || [];

                    if(user.length >= 1){
                        $.cookie.json = true;
                        var products = this.products;
                        var ckproducts = [];
                        // console.log(products);
                        $(".ck_prod:checked").each(function(){
                            var id = $(this).parents(".item-list").find(".id").text();
                            var product = products.filter(function(prod){
                                return (prod.id === id)
                            })[0];
                        ckproducts.push(product);
                        })
                        $.cookie("settlement",ckproducts,{path:"/"});
                        location = "/html/confirm.html";
                        this.delCkHandler();
                    }else{
                        location = "/html/login.html";
                    }
                    

                } 
            },
        }

        new Cart();
    });
});